package com.hspedu.generic.exercise;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

/**
 * @author DL5O
 * @version 1.0
 */
public class GenericExercise01 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("小龙23", 5000, new MyDate(2000, 9, 9)));
        employees.add(new Employee("大龙23", 6000, new MyDate(2001, 10, 9)));
        employees.add(new Employee("小张", 7500, new MyDate(2002, 11, 9)));
        employees.add(new Employee("小宋", 6500, new MyDate(2001, 5, 9)));

        System.out.println("====排序前====");
        for (Employee e : employees) {
            System.out.println(e);
            System.out.println();
        }

        System.out.println("====进行排序====");
        //进行排序操作
        //按照name进行排序，如果name相同，就按照生日的先后顺序进行排序
        Collections.sort(employees, new Comparator<Employee>() {
            @Override
            public int compare(Employee e1, Employee e2) {
//                int ret = e1.getName().compareTo(e2.getName());
                int ret = e1.getName().length() - e2.getName().length();
                //长度也按从小到大来比较
                if (ret != 0) {
                    return ret;
                }

                //如果名字/名字长度相同
                //出生年月按从大到下进行比较
                //下面是对birthday的比较，因此，我们最好把这个比较，放在MyDate类完成
                //封装后,将来可维护性和复用性，就大大增强
                MyDate e1Birthday = e1.getBirthday();
                MyDate e2Birthday = e2.getBirthday();
                return e1Birthday.compareTo(e2Birthday);
            }
        });

        Iterator<Employee> iterator = employees.iterator();
        while (iterator.hasNext()) {
            Employee e = iterator.next();
            System.out.println(e);
            System.out.println();
        }
    }

    /*//对ArrayList 集合中的元素进行排序
    public static void sort(ArrayList<Employee> employees){

    }*/
}

class Employee {
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "姓名:" + name + "\n工资:" + sal + "\n出生日期:" + birthday;
    }
}

class MyDate implements Comparable<MyDate> {
    private int year;
    private int month;
    private int day;


    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return year + "." + month + "." + day;
    }

    @Override
    public int compareTo(MyDate o) {//把对年月日year-month-day的比较放在这里
        /*if (year > o.getYear()) {
            return 1;
        } else if (month > o.getMonth()) {
            return 1;
        } else if (day > o.getDay()) {
            return 1;
        } else if (year < o.getYear()
                || month < o.getMonth() ||
                day < o.getDay()) {
            return -1;
        } else {
            return 0;
        }*/

        //老师的思路
        //如果出生年份不一样
        int yearMinus = year - o.getYear();
        if (yearMinus != 0) {
            return yearMinus;
        }

        //如果出生年份一样，但月份不一样
        int monthMinus = month - o.getMonth();
        if (monthMinus != 0) {
            return monthMinus;
        }

        //如果出生年份月份一样，就比较日期
        int dayMinus = day - o.getDay();
        return dayMinus;
    }
}

